﻿Public Class MainForm
    Private m_objdrawingsurface As Bitmap

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        m_objdrawingsurface = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height, Drawing.Imaging.PixelFormat.Format24bppRgb)
        initializeSurface()

    End Sub


    Private Sub initializeSurface()
        Dim objGraphics As Graphics
        Dim rectbounds As Rectangle
        objGraphics = Graphics.FromImage(m_objdrawingsurface)
        objGraphics.Clear(System.Drawing.SystemColors.Control)

        rectbounds = New Rectangle(0, 0, m_objdrawingsurface.Width, m_objdrawingsurface.Height)
        rectbounds.Inflate(-1, -1)
        objGraphics.DrawEllipse(System.Drawing.Pens.Orange, rectbounds)

    End Sub

    Private Sub MainForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim objgraphics As Graphics
        objgraphics = e.Graphics
        objgraphics.DrawImage(m_objdrawingsurface, 0, 0, m_objdrawingsurface.Width, m_objdrawingsurface.Height)
    End Sub

    Private Sub btnDrawText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawText.Click
        Dim objgraphics As Graphics
        Dim objfont As Font
        Dim intfontsize As Integer
        Dim inttextx As Integer
        Dim inttexty As Integer
        Dim objrandom As System.Random

        If txtInput.Text = "" Then Exit Sub

        objgraphics = Graphics.FromImage(m_objdrawingsurface)
        objrandom = New Random(Now.Millisecond)
        intfontsize = objrandom.Next(8, 48)
        inttextx = objrandom.Next(0, Me.ClientRectangle.Width - 20)
        inttexty = objrandom.Next(0, Me.ClientRectangle.Height - 20)
        objfont = New System.Drawing.Font("Arial", intfontsize, FontStyle.Bold Or FontStyle.Italic)
        objgraphics.DrawString(txtInput.Text, objfont, System.Drawing.Brushes.Red, inttextx, inttexty)

        objgraphics.Dispose()
        Me.Invalidate()

    End Sub
End Class
